//
//  WYMediaCell.m
//  CirclePush
//
//  Created by admin on 2018/4/9.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYMediaCell.h"
#import "WYMediaCollectionCell.h"
@interface WYMediaCell()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
//@property (nonatomic,strong)NSArray *nameArray;

@property (strong, nonatomic) IBOutlet UIView *hideView;



@end
@implementation WYMediaCell

//-(NSArray *)nameArray
//{
//    if (!_nameArray) {
//        _nameArray = @[@"畅达",@"华铁",@"航美",@"男人帮",@"今日头条",@"华铁",@"航美",@"男人帮",@"今日头条",@"知乎"];
//    }
//    return _nameArray;
//}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setUpUI];
    // Initialization code
}
-(void)setDataArray:(NSArray *)dataArray
{
    
    _dataArray = dataArray;
    if (!dataArray.count) {
        self.hideView.hidden = YES;
    }else{
        self.hideView.hidden = NO;
    }
  
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    params[@"key"] = @(self.collectionView.collectionViewLayout.collectionViewContentSize.height);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"editCollectionViewHeight" object:params];
    NSLog(@"---------------%f-----------------",self.collectionView.collectionViewLayout.collectionViewContentSize.height);
    
      [self.collectionView reloadData];
}
-(void)setUpUI
{
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize=CGSizeMake((kScreenWidth-50)/5, kScreenWidth/5);
    //横向最小距离
    layout.minimumInteritemSpacing=1.0f;
    layout.minimumLineSpacing=1.0f;
    //cell距离上  左  下 右 的距离
    layout.sectionInset=UIEdgeInsetsMake(5, 10, 5, 10);
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [self.collectionView setCollectionViewLayout:layout];
    [self.collectionView registerNib:[UINib nibWithNibName:@"WYMediaCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"WYMediaCollectionCell"];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    WYMediaCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WYMediaCollectionCell" forIndexPath:indexPath];
    
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYMediaCollectionCell" owner:nil options:nil].firstObject;
    }
    cell.nameLabel.text = self.dataArray[indexPath.row];
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
   CGFloat width = [self calculateRowWidth:self.dataArray[indexPath.row]];
    return CGSizeMake(width, 30);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}
- (CGFloat)calculateRowWidth:(NSString *)string {
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};  //指定字号
    CGRect rect = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, 30)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    return rect.size.width+20;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    NSString *key = self.dataArray[indexPath.row];
    params[@"key"]=key;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"clickHotMedia" object:params];
}

- (IBAction)clickMoreBtn:(id)sender {
    
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    NSString *key =@"";
    params[@"key"]=key;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"clickHotMedia" object:params];
    
}








@end
